package com.ftx.controller.test.annotation.profile;

import com.ftx.controller.test.annotation.importTest.Yellow;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;

/**
 *
 * @Profile
 *          spring 为我们提供的一个根据当前的环境，来动态激活和切换一些力组件的功能。
 *
 *
 * 一般都是对数据源
 *  开发环境，测试环境的处理
 *
 *
 * @Profile 指定组件在哪个环境的情况下才能被注册到容器中，不指定任何环境下都能注册这个组件
 *
 * 1.加了 Profile 注解，只有在这个环境被激活的时候才能加载这个bean，默认为：@Profile("default") 这个环境
 *  2. 写在配置类上，只有指定对应的环境时，这个配置类才能够被加载。
 * Created by fengtingxin on 2020/4/24.
 */

//@Profile("test")
//@PropertySource("classpath:/dbconfig.properties")
//@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware {

    @Value("${db.user}")
    private String user;

    private String dirverClass;

//    @Profile("test")
    @Bean
    public Yellow Yellow(){
        return new Yellow();
    }


    @Profile("dev")
    @Bean("devDataSource")
    public DataSource dataSourceDev() throws Exception{
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword("123456");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        comboPooledDataSource.setDriverClass(dirverClass);

        return comboPooledDataSource;
    }

    @Profile("test")
    @Bean("testDataSource")
    public DataSource dataSourceTest() throws Exception{
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword("123456");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        comboPooledDataSource.setDriverClass(dirverClass);
        return comboPooledDataSource;
    }

    @Profile("prod")
    @Bean("prodDataSource")
    public DataSource dataSourceProd(@Value("${db.password}") String password) throws Exception{
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/fengtingxin");
        comboPooledDataSource.setDriverClass(dirverClass);
        return comboPooledDataSource;
    }

    /**
     * Set the StringValueResolver to use for resolving embedded definition values.
     *
     * @param resolver
     */
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        dirverClass = resolver.resolveStringValue("${com.mysql.jdbc.Driver}");
    }
}
